<?php
/**
 * @description 用户管理
 * @author 张鹏  zhangpeng@tvmining.com
 * @version 1.0
 * @date 2017-04-19
 * @copyright Copyright (c) 2008 Tvmining (http://www.tvmining.com)
 */
namespace App\Services\Stat;

use App\Contracts\UdataContract;
use LRedis;
use Common;
use Exception;

date_default_timezone_set('PRC');

class Udata implements UdataContract
{
    public function test()
    {
        $a = LRedis::hgetall('user_card_sjh58f741dfbc89fa2c87980d64');
        dd($a);
    }

    /**
    * 缺失道具数据
    *
    * @return 
    */
    public function toolsLost($tvmid, $stime, $etime)
    {
        $action = array(
            'gamer_getPurchaseCardAllCount' => '制造和收获的数量',
            'gamer_getBuyCardAllCount' => '购买的数量',
            'gamer_getUseCardAllCount' => '使用的数量'
            );

        foreach ($action as $key => $value) {
            $urlArr[] = $this->getToolsData($key, $tvmid, $stime, $etime);
        }
        return $urlArr;
    }

    /**
    * 获取缺失道具数据
    *
    * @return 
    */
    public function getToolsData($type, $tvmid, $stime, $etime)
    {
        $gamestatHost = env('GAMESTAT_HOST');
        $cardtypeArr = [1,2,3,4,5,6];//1：炮弹（攻击卡）；2：护盾（防御卡）3：抢夺卡；4：导弹；5：修复卡；6：太阳（抽奖）
        $url = array();
        switch ($type) {
            // 制造和收获的数量
            case 'gamer_getPurchaseCardAllCount':
                foreach ($cardtypeArr as $key => $value) {
                    $url[$value] = $gamestatHost.':7000/'.$type.'?ownerid='.$tvmid.'&cardtype='.$value.'&stime='.$stime.'&etime='.$etime;
                }
                break;
            // 购买的数量
            case 'gamer_getBuyCardAllCount':
                foreach ($cardtypeArr as $key => $value) {
                    $url[$value] = $gamestatHost.':7000/'.$type.'?ownerid='.$tvmid.'&cardtype='.$value.'&stime='.$stime.'&etime='.$etime;
                }
                break;
            // 使用的数量
            case 'gamer_getUseCardAllCount':
                foreach ($cardtypeArr as $key => $value) {
                    $url[$value] = $gamestatHost.':7000/'.$type.'?opponentid='.$tvmid.'&cardtype='.$value.'&stime='.$stime.'&etime='.$etime;
                }
                break;
            // 应余的数量
            case '':
                
                break;
            // 现余的数量
            case '':
                
                break;
            
            default:
                
                break;
        }
        $ret = Common::concurrent($url);
        $ret = $this->getData($ret);
        return $ret;
    }

    /**
    * 处理道具数据
    *
    * @return 
    */
    public function getData($data)
    {
        $res = [];
        foreach ($data as $key => $value) {
            if (empty($value)) throw new Exception("gameAPI请求失败", 500);
            $value = json_decode($value, true);
            if ($value['status'] != 'success') throw new Exception("gameAPI请求失败", 500);
            $res[$key] = $value['data'];
        }
        return $res;
    }

    /**
    * 计算用户道具金额和冻结金额差值
    *
    * @return 
    */
    public function checkBid($tvmid)
    {
        $data = LRedis::hgetall('user_card_'.$tvmid);
        $bid = 0;//背包里有的
        foreach ($data as $key => $value) {
            if ($key == 'c02' || $key == 'c04') {
                $bid += $value * 200;
            }
            if ($key == 'c03') {
                $bid += $value * 100;
            }
        }
        unset($key,$value);
        $biding = 0;
        $build = LRedis::zrange('user_building_'.$tvmid, 0, -1);
        $keyArr = array('1'=>'c02', '2'=>'c03', '4'=>'c04');
        foreach ($build as $key => $value) {
            for ($i=1; $i < 18; $i++) {
                foreach ($keyArr as $k => $val) {
                    $building = LRedis::hget('product_technology_doing', $value.'_'.$i.'_'.$k);
                    if (!empty($building)) {
                        $building = json_decode($building, true);
                        if ($k == '1' || $k == '4') {
                            $biding += $building['amount'] * 200;
                        }
                        if ($k == '2') {
                            $biding += $building['amount'] * 100;
                        }
                    }
                }
            }
        }
        $total = $bid + $biding;
        unset($data);

        // $url = 'http://10.66.103.192/gold-interface-dfw/multi_gold_trade/query?tvmid='.$tvmid;
        // $data = Common::get($url);
        // $data = json_decode($data, true);
        // if ($data['status'] != 'success') throw new Exception("请求extraaAPI失败", 500);
        // $extra = $data['data']['extra'];
        $extra = 200;
        $array = array('extra'=>$extra, 'total'=>$total);
        return $array;
    }

    /**
    * 添加道具
    *
    * @return 
    */
    public function editCard($tvmid, $type, $value)
    {
        $keyArr = array('1'=>'c02', '2'=>'c03', '4'=>'c04', '6'=>'c06');
        $cardNum = LRedis::hget('user_card_'.$tvmid, $keyArr[$type]);//dd($cardNum);
        if (!$cardNum) $cardNum = 0;
        $num = $cardNum + $value;
        $in = LRedis::hset('user_card_'.$tvmid, $keyArr[$type], $num);
        Common::logs('editCard', $tvmid.'   '.$keyArr[$type].'   '.$num.'   old:'.$cardNum);
        return 'success';
    }
}